import jsSha1 from 'js-sha1'
/**
 * getFileHash 获取文件hash值
 * 函数参数说明
 * file 可以传一个文件 或者一个 文件片段集
 * type 1-> 源文件 2->二进制文件集合 默认为1
*/
export function getFileHash (file: any, type = 1) {
    let jsSha: any = jsSha1
    return new Promise( async (resolve, reject) => {
        if (type === 1) {
            let hash = jsSha.create()
            let reader = new FileReader()
            reader.readAsArrayBuffer(file)
            reader.onload = (e: any) => {
                hash.update(e.target.result)
                resolve(hash.hex())
            }
        } else {
            let hash = jsSha.create()
            let files = [...file]
            for (let i = 0; i < files.length; i++) {
                let data = await files[i].chunk.arrayBuffer()
                hash.update(data)
            }
            resolve(hash.hex())
        }
    }).catch((err) => {
        console.log('获取文件hash时遇到了错误：', err)
        Promise.reject(err)
    })
}

// 将对象所有属性清空
export function clearProperty (obj: objectType<string>) {
    // 获取属性数组
    let propertyArr: string[] = Object.keys(obj)
    propertyArr.forEach((item: string) => {
        obj[item] = ''
    })
    return obj
}

// 判断类型
export function isType (val: any) {
    let isType = Object.prototype.toString
    return isType.call(val).slice(8, -1)
}

// 初始化对象
export function initObj (originObj: objectType<any>, defaultObj?: objectType<any>) {
    let list = Object.entries(originObj)
    list.forEach((item) => {
        let type = isType(item[1])
        switch (type) {
            case 'Number':
                if (defaultObj?.number) {
                    originObj[item[0]] = defaultObj.number[item[0]] ?? defaultObj.number.default
                } else {
                    originObj[item[0]] = 0
                }
                break
            case 'String':
                if (defaultObj?.string) {
                    originObj[item[0]] = defaultObj.string[item[0]] ?? defaultObj.string.default
                } else {
                    originObj[item[0]] = ''
                }
                break
            case 'Array':
                if (defaultObj?.array) {
                    originObj[item[0]] = defaultObj.array[item[0]] ?? defaultObj.array.default
                } else {
                    originObj[item[0]] = []
                }
                break
            case 'Object':
                if (defaultObj?.object) {
                    originObj[item[0]] = defaultObj.object[item[0]] ?? defaultObj.object.default
                } else {
                    originObj[item[0]] = {}
                }
                break
            case 'Function':
                if (defaultObj?.function) {
                    originObj[item[0]] = defaultObj.function[item[0]] ?? defaultObj.function.default
                } else {
                    originObj[item[0]] = function () {}
                }
                break
            default:
                originObj[item[0]] = undefined
        }
    })
}

// 时间格式化
export function formatDate (date: string) {
    let dates = new Date(date)
    let y = dates.getFullYear()
    let m = dates.getMonth() + 1
    let d = dates.getDate()
    let h = dates.getHours()
    let min = dates.getMinutes()
    let s = dates.getMinutes()
    return `${y}/${timeFill(m)}/${timeFill(d)} ${timeFill(h)}:${timeFill(min)}:${timeFill(s)}`
}
// 时间格式化-1
export function formatDates (format: string, join = '/', date?: string) {
    let time = ''
    let dates = date ? new Date(date) : new Date()
    let y = dates.getFullYear()
    let m = dates.getMonth() + 1
    let d = dates.getDate()
    let h = dates.getHours()
    let min = dates.getMinutes()
    let s = dates.getMinutes()
    if (format === 'date') {
        return `${y}${join}${timeFill(m)}${join}${timeFill(d)}`
    }
    if (format === 'dateTime') {
        return `${y}${join}${timeFill(m)}${join}${timeFill(d)} ${timeFill(h)}:${timeFill(min)}:${timeFill(s)}`
    }
    if (format === 'time') {
        return `${timeFill(h)}:${timeFill(min)}:${timeFill(s)}`
    }
}

// 小于10加0
function timeFill (time: number) {
    return Number(time) > 9 ? time : '0' + time
}